package com.engine4cloud.tdf.supervision.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.engine4cloud.tdf.common.core.exception.ValidateCodeException;
import com.engine4cloud.tdf.common.data.web.query.Pages;
import com.engine4cloud.tdf.common.data.web.query.Queries;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.supervision.convert.TerminalOccasionSignConvert;
import com.engine4cloud.tdf.supervision.dto.create.RcglOccasionSignCreateDTO;
import com.engine4cloud.tdf.supervision.dto.query.RcglOccasionSignQueryDTO;
import com.engine4cloud.tdf.supervision.dto.result.RcglOccasionSignResultDTO;
import com.engine4cloud.tdf.supervision.entity.JzdxEntity;
import com.engine4cloud.tdf.supervision.entity.RcglOccasionSignEntity;
import com.engine4cloud.tdf.supervision.mapper.RcglOccasionSignMapper;
import com.engine4cloud.tdf.supervision.service.JzdxService;
import com.engine4cloud.tdf.supervision.service.RcglOccasionSignService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * 矫正对象现场签到
 *
 * @author wenjie
 * @date 2024-07-24 17:23:37
 */
@Service
public class RcglOccasionSignServiceImpl extends ServiceImpl<RcglOccasionSignMapper, RcglOccasionSignEntity> implements RcglOccasionSignService {
    @Resource
    private JzdxService jzdxService;

    @Override
    public PageInfo<RcglOccasionSignResultDTO> pageList(RcglOccasionSignQueryDTO dto) {
        IPage<RcglOccasionSignEntity> page = super.page(Pages.page(dto), wrapper(dto));
        return Pages.convert(page, TerminalOccasionSignConvert.INSTANCE::convert);
    }

    @Override
    public RcglOccasionSignResultDTO getById(Long id) {
        return TerminalOccasionSignConvert.INSTANCE.convert(super.getById(id));
    }

    @Override
    public Boolean create(RcglOccasionSignCreateDTO dto) {
        JzdxEntity dx = Optional.ofNullable(jzdxService.getByIdentityNotRelease(dto.getIdentity()))
                .orElseThrow(() -> new ValidateCodeException("矫正对象数据不存在"));
        RcglOccasionSignEntity entity = TerminalOccasionSignConvert.INSTANCE.create(dto, dx.getTenantId());
        entity.setRybh(dx.getRybh());
        return super.save(entity);
    }

    private LambdaQueryWrapper<RcglOccasionSignEntity> wrapper(RcglOccasionSignQueryDTO query) {
        LambdaQueryWrapper<RcglOccasionSignEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(RcglOccasionSignEntity::getId, v));
        Queries.accept(query.getRybh(), v -> wrapper.eq(RcglOccasionSignEntity::getRybh, v));
        Queries.accept(query.getDeviceId(), v -> wrapper.eq(RcglOccasionSignEntity::getDeviceId, v));
        Queries.accept(query.getSignType(), v -> wrapper.eq(RcglOccasionSignEntity::getSignType, v));
        return wrapper;
    }
}